package com.mohan.aop.impl;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class LinuxCondition implements Condition {
    /**
     *
     * @param context 判断条件能适应的上下文（环境）
     * @param metadata 当前标注了@Conditional注解的注释信息
     * @return
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // 获取到bean的创建工厂（获取到IOC容器使用到的BeanFactory,他就是创建对象以及进行装配的工厂）
        final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        // 获取类加载器
        final ClassLoader classLoader = context.getClassLoader();
        // 获取操作环境
        final Environment environment = context.getEnvironment();
        // 获取到bean定义的注册类
        final BeanDefinitionRegistry registry = context.getRegistry();

        // 我们可以在这里可以有更多的判断，就像下面一样，如果Spirng容器中
        final boolean definition = registry.containsBeanDefinition("person");
        System.out.println("definition = " + definition);

        final String property = environment.getProperty("os.name");
        if ("linux".equals(property)) return true;
        return false;
    }
}
